****** This do-file reproduces main results in the paper ******
****** Last edited: July 12, 2024				        ******
****** Amy Liu and Jay Kao

************************
*Preface*
************************

*ssc install coefplot, replace
*ssc install conjoint, replace
*ssc install schemepack, replace
*set install grc1leg,from( http://www.stata.com/users/vwiggins/)

clear
graph drop _all
set more off

use "immigration.dta", clear

drop if selected_immigration ==. | selected_nih ==. |  selected_mother_tongue==.


/* Figure 4: Marginal Means */
conjoint selected_immigration country ethnic visa mandarin, est(mm) id(record)
matrix admission_mm = e(results)

conjoint selected_nih country ethnic visa mandarin, est(mm) id(record)
matrix nih_mm = e(results)

conjoint selected_mother_tongue country ethnic visa mandarin, est(mm) id(record)
matrix language_mm = e(results)

coefplot ///
	(matrix(admission_mm[,1]), label(Admission) msymbol(o) mcolor(black) ciopts(lc(black))) ///
	(matrix(nih_mm[,1]), label(Health Insurance) msymbol(d) mcolor(black) ciopts(lc(black))) ///
	(matrix(language_mm[,1]), label(Mother Tongue) msymbol(t) mcolor(black) ciopts(lc(black))) ///
	, ci((5 6)) keep(*:) xline(0.5, lpattern(-) lcolor(black)) ///
	coeflabels( No_Info= "No Info"  Vietnam= "Vietnam"  Hong_Kong= "Hong Kong"  China= "China"  Ukraine= "Ukraine"  No_Info= "No Info"  Hakka= "Hakka"  Cantonese= "Cantonese" ///
	No_Info= "No Info"  Spouse= "Spouse"  Blue_Collar= "Blue Collar"  White_Collar= "White Collar"  Refugee= "Refugee"  No_Info= "No Info"  Cannot_Speak= "Cannot Speak"  Can_Speak= "Can Speak" ) ///
	eqlabels( "{bf:Country of origin}" "{bf:Ethnicity}" "{bf:Occupation/visa status}" "{bf:Mandarin proficiency}", asheadings) ///
	graphregion(col(white)) scale(0.7) xtitle({bf:Estimated MMs}) legend(row(1) pos(6))  xlabel(0.3(0.1)0.7)


	
/* Appendix SI.1: AMCEs  */
conjoint selected_immigration country ethnic visa mandarin, est(amce) id(record) constraint(country#ethnic) base(1 2 4 1)
matrix results1 = e(results)

conjoint selected_nih country ethnic visa mandarin, est(amce) id(record) constraint(country#ethnic) base(1 2 4 1)
matrix results2 = e(results)

conjoint selected_mother_tongue country ethnic visa mandarin, est(amce) id(record) constraint(country#ethnic) base(1 2 4 1)
matrix results3 = e(results)

coefplot ///
	(matrix(results1[,1]), label(Admission) msymbol(o) mcolor(black) ciopts(lc(black))) ///
	(matrix(results2[,1]), label(Health Insurance) msymbol(d) mcolor(black) ciopts(lc(black))) ///
	(matrix(results3[,1]), label(Mother Tongue) msymbol(t) mcolor(black) ciopts(lc(black))) ///
	, ci((5 6)) keep(*:) xline(0, lpattern(-) lcolor(black)) coeflabels( No_Info= "No Info"  Vietnam= "Vietnam"  ///
	Hong_Kong= "Hong Kong"  China= "China"  Ukraine= "Ukraine"  No_Info= "No Info"  Hakka= "Hakka"  Cantonese= "Cantonese"  No_Info= "No Info"  ///
	Spouse= "Spouse"  Blue_Collar= "Blue Collar"  White_Collar= "White Collar"  Refugee= "Refugee"  No_Info= "No Info"  Cannot_Speak= "Cannot Speak"  Can_Speak= "Can Speak" ) ///
	eqlabels( "{bf:Country of origin}" "{bf:Ethnicity}" "{bf:Occupation/visa status}" "{bf:Mandarin proficiency}", asheadings) graphregion(col(white)) scale(0.7) xtitle({bf:Estimated AMCEs}) ///
	order(Vietnam Hong_Kong China Ukraine Cantonese Hakka Refugee Spouse Blue_Collar White_Collar Cannot_Speak Can_Speak) legend(row(1) pos(6)) xlabel(-0.3(0.1)0.3)




/* Interaction (Figure 5 & 6) */

* Figure 5: Ethnicity X linguistic ability (marginal means)
conjoint selected_immigration ethnic, est(mm) id(record) subg(mandarin)
matrix mandarin_can = e(results_Can_Speak)
matrix mandarin_cant = e(results_Cannot_Speak)

conjoint selected_nih ethnic, est(mm) id(record) subg(mandarin)
matrix mandarin_can_nih = e(results_Can_Speak)
matrix mandarin_cant_nih = e(results_Cannot_Speak)

conjoint selected_mother_tongue ethnic, est(mm) id(record) subg(mandarin)
matrix mandarin_can_tongue = e(results_Can_Speak)
matrix mandarin_cant_ongue = e(results_Cannot_Speak)

coefplot (matrix(mandarin_can[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(mandarin_cant[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Admission) || ///
		 (matrix(mandarin_can_nih[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(mandarin_cant_nih[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Healthcare) || ///
		 (matrix(mandarin_can_tongue[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) ///
		 (matrix(mandarin_cant_ongue[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Mother Tongue) ||, ///
		 ci((5 6)) ///
		 xline(0.5, lpattern(-) lcolor(black)) graphregion(col(white)) ///
		 xtitle({bf:Estimated Marginal Means}) scheme(white_tableau) byopts(compact cols(3) legend(position(1))) legend(rows(1)) ///
		 scale(0.7) coeflabels(1."No_Info" = "No Info") order(Hakka Cantonese No_Info)

		 
		 
		 
* Figure 6: National origin X linguistic ability (marginal means)		 
conjoint selected_immigration country ethnic visa, est(mm) id(record) subg(mandarin)
matrix mandarin_can = e(results_Can_Speak)
matrix mandarin_cant = e(results_Cannot_Speak)

conjoint selected_nih country ethnic visa, est(mm) id(record) subg(mandarin)
matrix mandarin_can_nih = e(results_Can_Speak)
matrix mandarin_cant_nih = e(results_Cannot_Speak)

conjoint selected_mother_tongue country ethnic visa, est(mm) id(record) subg(mandarin)
matrix mandarin_can_tongue = e(results_Can_Speak)
matrix mandarin_cant_ongue = e(results_Cannot_Speak)

coefplot (matrix(mandarin_can[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(mandarin_cant[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Admission) || ///
		 (matrix(mandarin_can_nih[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(mandarin_cant_nih[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Healthcare) || ///
		 (matrix(mandarin_can_tongue[,1]), label(Can Speak) msymbol(o) mcolor(black) ciopts(lc(black))) ///
		 (matrix(mandarin_cant_ongue[,1]), label(Cannot Speak) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Mother Tongue) ||, ///
		 ci((5 6)) drop(*."No_Info") ///
		 xline(0.5, lpattern(-) lcolor(black)) graphregion(col(white)) ///
		 xtitle({bf:Estimated Marginal Means}) scheme(white_tableau) byopts(compact cols(3) legend(position(1))) legend(rows(1)) ///
		 scale(0.7) ///
		 coeflabels(1."Hong_Kong" = "Hong Kong")

		 
		 
/* Subgroup preference */

/* Figure 7: Immigrant preferences by Bensheng/Waisheng ID */
gen bensheng = .
replace bensheng = 0 if father_mainlander == 1
replace bensheng = 1 if father_hakka == 1 | father_minnan == 1 | father_aborigine == 1 


conjoint selected_immigration country ethnic visa mandarin, est(mm) id(record) subg(bensheng)
matrix waisheng_results = e(results__0)
matrix bensheng_results = e(results__1)

conjoint selected_nih country ethnic visa mandarin, est(mm) id(record) subg(bensheng)
matrix waisheng_results_nih = e(results__0)
matrix bensheng_results_nih = e(results__1)

conjoint selected_mother_tongue country ethnic visa mandarin, est(mm) id(record) subg(bensheng)
matrix waisheng_results_tongue = e(results__0)
matrix bensheng_results_tongue = e(results__1)


coefplot (matrix(bensheng_results[,1]), label(Benshengren ID) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(waisheng_results[,1]), label(Waishengren ID) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 , bylabel(Admission) || ///
		 (matrix(bensheng_results_nih[,1]), label(Benshengren ID)) ///
		 (matrix(waisheng_results_nih[,1]), label(Waishengren ID)) ///
		 , bylabel(Healthcare) || ///
		 (matrix(bensheng_results_tongue[,1]), label(Benshengren ID)) ///
		 (matrix(waisheng_results_tongue[,1]), label(Waishengren ID)) ///
		 , bylabel(Mother Tongue) ||, ///
		 ci((5 6)) keep(*."China") ///
		 legend(row(1) pos(6)) vertical yline(0.5, lpattern(-) lcolor(black)) graphregion(col(white)) ///
		 xtitle({bf:Estimated Marginal Means}) scheme(white_tableau) byopts(compact cols(3)) scale(0.7)



/* Appendix SI.5: Immigrant preference by party ID */
gen party_new =.
replace party_new = 1 if party == "KMT" | party == "NP" | party == "PFP"
replace party_new = 2 if party == "DPP" | party == "TSU" | party == "Green Party" | party == "NPP" | party == "SDP" | party == "TSP"
replace party_new = 3 if party == "TPP" | party == "I do not support any of these party"
label define party_new_label 1 "blue" 2 "green" 3 "independent"
label values party_new party_new_label

conjoint selected_immigration country ethnic visa mandarin, est(mm) id(record) subg(party_new)
matrix party_blue = e(results_blue)
matrix party_green = e(results_green)
matrix party_independent = e(results_independent)

conjoint selected_nih country ethnic visa mandarin, est(mm) id(record) subg(party_new)
matrix party_blue_nih = e(results_blue)
matrix party_green_nih = e(results_green)
matrix party_independent_nih = e(results_independent)

conjoint selected_mother_tongue country ethnic visa mandarin, est(mm) id(record) subg(party_new)
matrix party_blue_tongue = e(results_blue)
matrix party_green_tongue = e(results_green)
matrix party_independent_tongue = e(results_independent)

coefplot (matrix(party_green[,1]), label(Pan-Green) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(party_blue[,1]), label(Pan-Blue) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 (matrix(party_independent[,1]), label(Nonpartisan) msymbol(square) mcolor(black) ciopts(lc(black))) /// 
		 , bylabel(Admission) || ///
		 (matrix(party_green_nih[,1]), label(Pan-Green) msymbol(o) mcolor(black) ciopts(lc(black))) /// 
		 (matrix(party_blue_nih[,1]), label(Pan-Blue) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 (matrix(party_independent_nih[,1]), label(Nonpartisan) msymbol(square) mcolor(black) ciopts(lc(black))) /// 
		 , bylabel(Healthcare) || ///
		 (matrix(party_green_tongue[,1]), label(Pan-Green) msymbol(o) mcolor(black) ciopts(lc(black))) ///
		 (matrix(party_blue_tongue[,1]), label(Pan-Blue) msymbol(t) mcolor(black) ciopts(lc(black))) ///
		 (matrix(party_independent_tongue[,1]), label(Nonpartisan) msymbol(square) mcolor(black) ciopts(lc(black))) /// 
		 , bylabel(Mother Tongue) ||, ///
		 ci((5 6)) keep(*."China") ///
		 legend(row(1) pos(6)) vertical yline(0.5, lpattern(-) lcolor(black)) graphregion(col(white)) ///
		 xtitle({bf:Estimated Marginal Means}) scheme(white_tableau) byopts(compact cols(3)) scale(0.7)


/* Note: Figures in Online Appendix can be fouind in R script */

clear
